import Axios from 'axios'

const state = {
	loading: [true],
	treeData: [],
	tableData: [],
	expandNodeId: [],
	rowId: null,		//修改项目的id
	parentId: [],		//上级类别id
	parentName: [],		//上级类别名称
	multipleSelection: [],
	selectRowId: [],
	row: []
}

const mutations = {
	updateExpandNodeId(state, id) {
		if(id) {
			state.expandNodeId.splice(0, 1, id);
		} else {
			state.expandNodeId.splice(0);
		}
	},
	updateRow(state, row) {
		state.row.splice(0, 1, row);
	},
	updateRowId(state, id) {
		state.rowId = id ? id : null;
	},
	updateParentId(state, id) {		//更新上级类别id
		state.parentId.splice(0, 1, id);
	},
	updateParentName(state, name) {		//更新上级类别名称
		state.parentName.splice(0, 1, name);
	},
	updateTreeData(state, arr) {
		let treeData = state.treeData,
			accountItems = null;
		treeData.length = 0;
		arr.forEach(item => {
			item['accountItems'] = item['accountItems'].filter(accountItem => accountItem['id'] !== -99999);		//过滤掉职员选项
			treeData.push(item);
		});
		this.commit('checkItemsStore/updateTableData', treeData);
	},
	updateTableData(state, arr) {			//更新表格数据
		let tableData = state.tableData,
            expandNodeId = state.expandNodeId[0];
        if(arr.length > 0) {
        	arr.forEach(item => {
        		if(item['id'] == expandNodeId) {
        			tableData.length = 0;
        			state.parentId.splice(0, 1, item['id']);			//更新上级类别id
	        		state.parentName.splice(0, 1, item['name']);		//更新上级类别名称
	        		if(item['accountItems'].length > 0) {
	        			item['accountItems'].forEach(accItem => {
	        				tableData.push(accItem);
	        			});
	        		} else {
	        			tableData.splice(0);
	        		}
        		} else {
        			this.commit('checkItemsStore/updateTableData', item['accountItems']);
        		}
        	});
        }
	},
	updateTableData2(state, arr) {
		let tableData = state.tableData;
		tableData.length = 0;
		arr.forEach(item => tableData.push(item));
	},
	multipleSelection(state, arr) {
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    }
}

const actions = {
	getTreeData({state, commit, dispatch}) {
		state.loading.splice(0, 1, true);
		Axios.get('accountItem/select').then(res => {
			if(res.data.code == 20001) {
				setTimeout(() => {
					state.loading.splice(0, 1, false);
				}, 300);
				if(res.data.data) {
					if(state.expandNodeId.length == 0) {		//默认展开树形第一个节点
						state.expandNodeId.splice(0, 1, res.data.data[0]['id']);
						state.row.splice(0, 1, res.data.data[0]);
						state.parentId.splice(0, 1, res.data.data[0]['id']);			//更新上级类别id
	        			state.parentName.splice(0, 1, res.data.data[0]['name']);		//更新上级类别名称
					}
					commit('updateTreeData', res.data.data);
				}
			} else {
				setTimeout(() => {
					state.loading.splice(0, 1, false);
				}, 300);
			}
		});
	}
}


export default {
	namespaced: true,
	state,
	mutations,
	actions
}